package com.xin.motion
{
/**
 * 缓动基类
 * @author daichangxin
 */
public class AnimationBase
{
	protected var _elapsed:uint;
	protected var _duration:uint;
	protected var _curve:Function;
	protected var _percentage:Number = 0;
	protected var _value:Number = 0;
	
	public function AnimationBase(duration:uint)
	{
		_duration = duration;
	}
	
	private function update():void
	{
		//计算时间比
		_percentage = _duration == 0? 1:_elapsed / _duration;
		_value = _curve == null? _percentage:_curve(_percentage);
		onValueUpdate();
	}
	
	/**
	 * 属性刷新
	 */
	protected function onValueUpdate():void{}
	
	/**
	 * 设置时间点
	 */
	public function set elapsed(value:uint):void
	{
		_elapsed = value;
		update();
	}
	/**
	 * 获取当前时间点
	 */
	public function get elapsed():uint
	{
		return _elapsed;
	}
	
	/**
	 * 设置动画长度
	 */
	public function set duration(value:uint):void
	{
		_duration = value;
		update();
	}
	/**
	 * 返回动画长度
	 */
	public function get duration():uint
	{
		return _duration;
	}
	
	/**
	 * 返回动画当前播放时间进度
	 */
	public function get percentage():Number
	{
		return _percentage;
	}
	
	/**
	 * t:[0-1], value = fx(t);  value:[0-1];
	 */
	public function get value():Number
	{
		return _value;
	}
	
	public function set curve(value:Function):void
	{
		_curve = value;
		update();
	}
	public function get curve():Function
	{
		return _curve;
	}
}
}
